package leetcode;

/**
 * description:
 * author:zt
 * date:2024-09-01
 */

/**
 * 给定二叉树的根节点 root ，返回所有左叶子之和。
 */
public class leetcode404 {


    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null) return 0;
        return resolveNode(root, false);
    }

    private int resolveNode(TreeNode node, boolean reft) {
        if (node.left == null && node.right == null && reft) return node.val;

        int res = 0;
        if (node.left != null) {
            res += resolveNode(node.left, true);
        }
        if (node.right != null) {
            res += resolveNode(node.right, false);
        }
        return res;
    }



    private class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
